package cn.com.ericsson;

/**
 * @author essnxxg
 * 
 */
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.ActiveEvent;
import java.awt.event.ActionListener;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Vector;
import java.util.concurrent.TimeUnit;

import javax.swing.JEditorPane;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JMenu;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.AbstractTableModel;

public class LogPreDeal extends JFrame {
	
	public static File root = null;
	public static Vector<File> isfFiles = null; //保存目录及其子目录下的所有isf文件
	
	private JPanel jp = new JPanel(new BorderLayout());
	private JPanel jp1 = new JPanel();

	public static SimpleModel tableData = new SimpleModel();
	
	private JMenu menus1 = new JMenu("File");
	private JMenu menus2 = new JMenu("Pre-Process");
	private JMenu menus3 = new JMenu("Log Process");
	private JMenu menus4 = new JMenu("Help");
	
	private JMenuItem item11 = new JMenuItem("Choose Folder");
	private JMenuItem item12 = new JMenuItem("Close");
	
	private JMenuItem item21 = new JMenuItem("Normalize Isf Name"); 
	private JMenuItem item22 = new JMenuItem("Auto-Create Folder"); 
	
	private JMenuItem item31 = new JMenuItem("Isf 2 Txt"); 
	
	private JTable table = null;
	private JScrollPane sp = null;
	
	private ActionListener al = new ActionListener()
	{
		public void actionPerformed(ActionEvent e)
		{
			String actionCommand = e.getActionCommand();
			
			if ("Choose Folder".equals(actionCommand))
			{
			     JFileChooser fc=new JFileChooser();
			     fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
			     int val=fc.showOpenDialog(LogPreDeal.this);
			     
			     if(val==JFileChooser.APPROVE_OPTION)
			     {
			    	 root = fc.getSelectedFile();	
			    	 
			    	 if (FileUtils.isPathIllegal(root.getPath()))
			    	 {
			    		 JOptionPane.showMessageDialog(null, "Current path is illegal!", "Confirmation Message", JOptionPane.INFORMATION_MESSAGE);
			    		 root = null;
			    		 isfFiles = null;
			    	 }		    	 
			    	 else
			    	 {
				    	 isfFiles = FileUtils.getAllIsf(root);

				    	 System.out.println("\n################### " + "Total number of isf files is: " + isfFiles.size() + " ###################");
				    	 System.out.println("################### All isf files list ###################\n");

				    	 for (int i=0; i<isfFiles.size(); i++)
				    	 {
				    		 tableData.addText(isfFiles.get(i).toString());		    		 
				    		 System.out.println(isfFiles.get(i).toString());
				    	 }	
			    	 }		    	 			    	 			    	 			    	 
			     }	
			}
			
			else if ("Normalize Isf Name".equals(actionCommand))
			{
				if (isfFiles == null)
				{
					JOptionPane.showMessageDialog(null, "Please choose a folder!", "Confirmation Message", JOptionPane.INFORMATION_MESSAGE);
				}
				else if (isfFiles.size() == 0)
				{
					System.out.println("There are no isf files!");
					JOptionPane.showMessageDialog(null, "No isf contained, please change a folder!", "Confirmation Message", JOptionPane.INFORMATION_MESSAGE);
				}
				else
				{
					System.out.println("\n################### Begin normalizing isf file name ###################\n");
//					FileUtils.changeIsfName(isfFiles);
					FileUtils.changeChinesePath(root);
					System.out.println("\n################### Normalizing is finished ###################\n");
				}
				
			}
			
			else if ("Auto-Create Folder".equals(actionCommand))
			{
				if (isfFiles == null)
				{
					System.out.println("No Folder to be created!");
				}
				else
				{
					procAllFilesInFolder(isfFiles);
				}
			}
			
			else if ("Isf 2 Txt".equals(actionCommand))
			{
				System.out.println(root.getPath());
				Log2txt.log2txt(root.getPath());
			}

//			tf.setText(((JMenuItem)e.getSource()).getText());
		}
	};
		
	
	public LogPreDeal()
	{		
		table = new JTable(tableData);
	
		sp = new JScrollPane();
		sp.getViewport().add(table);
		
		jp.setBackground(Color.BLUE);
		setContentPane(jp);
		jp.add(BorderLayout.NORTH, new JLabel("All isf list"));
		jp.add(BorderLayout.SOUTH, sp);
		
		item11.addActionListener(al);
		item21.addActionListener(al);
		item22.addActionListener(al);
		item31.addActionListener(al);

		menus1.add(item11);
		menus1.add(item12);
		menus2.add(item21);
		menus2.add(item22);
		menus3.add(item31);

		JMenuBar mb = new JMenuBar();
		mb.add(menus1);
		mb.add(menus2);
		mb.add(menus3);
	
		
		setJMenuBar(mb);
		setLayout(new FlowLayout());
	}
	 
	 
	 
	 public static void run(final JFrame f, final int width, final int height)
	 {
		 SwingUtilities.invokeLater(new Runnable(){
			 public void run(){
				 f.setTitle("LTE Process Machine");
				 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				 f.setSize(width, height);
				 f.setVisible(true);
			 }
		 });
	 }
	 
	 public void procAllFilesInFolder(Vector<File> isfs)
	 {
		 int totalNum = isfs.size();

		 File folder = null;
		 File parent = null;
		 
		 for (int i=0; i<totalNum; i++)
		 {
			 parent = isfs.get(i).getParentFile();		 
			 String newFolder = parent.getPath() + "\\" + FileUtils.getFileName(isfs.get(i));
			 folder = new File(newFolder);  
			 folder.mkdir();
			 FileUtils.moveTo(folder, isfs.get(i)); 
		 }
	 }
	 
	 
	 public static void main(String[] args) throws Exception{		 
		 FileUtils.getFilterMap();
		 run(new LogPreDeal(), 500, 800);
	 }
	 	 
	
}

